// pages/personInfo/personInfo.js
var request = require("../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getData();

  },
  // 获取数据
  getData: function () {
    var that = this;
    var url = request.dataUrl + '/api/user/get_user';
    var params = {
      session3rd: wx.getStorageSync('session3rd')
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          lis: res
        })
      } 

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })
    })
  },
  formSubmit(e) {
    // console.log(e)
    var add = e.detail.value.add
    var area = e.detail.value.area
    var email = e.detail.value.email
    var idcard = e.detail.value.idcard
    var major = e.detail.value.major
    var name = e.detail.value.name
    var phone = e.detail.value.phone
    var school = e.detail.value.school

    if (add == '' || area == '' || email == '' || idcard == "" || major == "" || name == "" || phone == "" || school == "") {
      wx.showToast({
        title: '输入框不能为空',
        image: '../../image/fail.png',
        duration: 1500
      })
    } else {
      var myreg = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9]|19[0-9])\d{8}$/;
      if (!myreg.test(phone)) {
        wx.showToast({
          title: '手机号有误',
          image: '../../image/fail.png',
          duration: 1500
        })
        return false;
      } else {
        var id_card =
          /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (!id_card.test(idcard)) {
          wx.showToast({
            title: '身份证号有误！',
            image: '../../image/fail.png',
            duration: 1500
          })
          return false;
        } else {
          var e_mail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
          if (!e_mail.test(email)) {
            wx.showToast({
              title: '邮箱有误',
              image: '../../image/fail.png',
              duration: 1500
            })
            return false;
          } else {
            wx.request({
              url: request.dataUrl + '/api/user/set_user',
              data: {
                session3rd: wx.getStorageSync('session3rd'),
                user_name: name,
                user_phone: phone,
                user_email: email,
                user_address: add,
                user_int_profession: major,
                user_int_school: school,
                user_int_address: area,
                user_number: idcard               
              },
              method: 'POST',
              success: function (res) {
                console.log(res)
                if (res.data.code == 0) {
                  wx.showToast({
                    title: res.data.msg,
                    image: '../../image/success.png',
                    duration: 1500,
                    success:function(){
                      setTimeout(function(){
                        wx.navigateBack()
                      },2000)                     
                    }
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    image: '../../image/fail.png',
                    duration: 1500
                  })
                }

              }
            })
          }

        }
      }
    }

  },
  amendInfo(e) {
    wx.navigateTo({
      url: '../amendInfo/amendInfo',
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    that.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})